/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.cax.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.cax.domain.bo.CaxConvertInfoBo;
import com.comac.ins.cax.domain.vo.CaxConvertInfoVo;
import com.comac.ins.cax.domain.vo.CaxConvertStatisticItem;
import com.comac.ins.cax.domain.vo.CaxConvertStatisticVo;
import com.comac.ins.cax.enums.CaxConstants;
import com.comac.ins.cax.service.ICaxConvertInfoService;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * CAX格式转换
 *
 * @author yk
 * @desc CAX格式转换
 * @date 2024-06-19 9:07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cax/convert")
public class CaxConvertInfoController extends BaseController {

    @Autowired
    private ICaxConvertInfoService caxConvertService;

    /**
     * CAX格式转换-本地文件
     *
     * @param file        转换文件
     * @param convertType 转换类型
     * @param inputType   输入类型
     * @param outputType  输出类型
     * @return 转换记录
     */
    @Log(title = CaxConstants.MODULE_CONVERT, businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/local")
    @SaCheckPermission("cax:convert:convert")
    public R<CaxConvertInfoVo> convert(@RequestParam("file") MultipartFile file,
                                       @RequestParam(value = "convertType") String convertType,
                                       @RequestParam(value = "inputType") String inputType,
                                       @RequestParam(value = "outputType") String outputType) {
        return R.ok(caxConvertService.convert(file, null, CaxConstants.FILE_SOURCE_TYPE_LOCAL, convertType, inputType, outputType));
    }

    /**
     * CAX格式转换-系统文件
     *
     * @param sourceFileId 源文件id
     * @param convertType  转换类型
     * @param inputType    输入类型
     * @param outputType   输出类型
     * @return 转换记录
     */
    @Log(title = CaxConstants.MODULE_CONVERT, businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/sys")
    @SaCheckPermission("cax:convert:convert")
    public R<CaxConvertInfoVo> convert(@RequestParam("sourceFileId") String sourceFileId,
                                       @RequestParam(value = "convertType") String convertType,
                                       @RequestParam(value = "inputType") String inputType,
                                       @RequestParam(value = "outputType") String outputType) {
        return R.ok(caxConvertService.convert(null, Long.valueOf(sourceFileId), CaxConstants.FILE_SOURCE_TYPE_SYS, convertType, inputType, outputType));
    }

    /**
     * CAX格式转换记录详情查询
     *
     * @param id 记录ID
     * @return 记录详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("cax:convert:getInfo")
    public R<CaxConvertInfoVo> getInfo(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return R.ok(caxConvertService.queryById(id));
    }

    /**
     * 转换记录列表查询
     *
     * @param bo 查询BO
     * @return TableDataInfo
     */
    @PostMapping("/list")
    @SaCheckPermission("cax:convert:list")
    public TableDataInfo<CaxConvertInfoVo> list(@RequestBody CaxConvertInfoBo bo) {
        return caxConvertService.queryPageList(bo);
    }

    /**
     * 批量删除
     *
     * @param ids id数组
     * @return 是否成功标志
     */
    @Log(title = CaxConstants.MODULE_CONVERT_INFO, businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaCheckPermission("cax:convert:delete")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(caxConvertService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 下载CAX格式转换-源文件
     *
     * @param response response
     * @param id       记录ID
     * @throws IOException 异常
     */
    @GetMapping("/download/source/{id}")
    @SaCheckPermission("cax:convert:downloadSource")
    public void downloadSource(HttpServletResponse response, @NotNull(message = "ID不能为空") @PathVariable("id") Long id) throws IOException {
        caxConvertService.downloadSource(response, id);
    }

    /**
     * 下载CAX格式转换-转换文件
     *
     * @param response response
     * @param id       记录ID
     * @throws IOException 异常
     */
    @GetMapping("/download/target/{id}")
    @SaCheckPermission("cax:convert:downloadTarget")
    public void downloadTarget(HttpServletResponse response, @NotNull(message = "ID不能为空") @PathVariable("id") Long id) throws IOException {
        caxConvertService.downloadTarget(response, id);
    }

    /**
     * 转换记录导出
     *
     * @param response
     * @return
     */
    @Log(title = CaxConstants.MODULE_CONVERT_INFO, businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    @SaCheckPermission("cax:convert:exportData")
    public void exportData(HttpServletResponse response, CaxConvertInfoBo bo) {
        caxConvertService.export(response, bo);
    }

    /**
     * 首页转换统计
     */
    @GetMapping("/statistic")
    @SaCheckPermission("cax:convert:statistic")
    public R<CaxConvertStatisticVo> statistic() {
        return R.ok(caxConvertService.convertStatistic());
    }

    /**
     * 首页转换统计-根据时间筛选
     */
    @GetMapping("/statisticByDate")
    @SaCheckPermission("cax:convert:statistic")
    public R<LinkedHashMap<Object, List<CaxConvertStatisticItem>>> statisticByDate(String startTime, String endTime) {
        return R.ok(caxConvertService.convertStatisticByDate(startTime, endTime));
    }

    /**
     * 根据文件路径下载文件
     *
     * @param response response
     * @param filePath 文件路径
     * @throws IOException 异常
     */
    @GetMapping("/download/path")
    @SaCheckPermission("cax:convert:downloadPath")
    public void downloadByPath(HttpServletResponse response, @NotNull(message = "path不能为空") @RequestParam("filePath") String filePath) throws IOException {
        caxConvertService.downloadByPath(response, filePath);
    }
}
